#include "rules.h"

QMap<QString, float> *rulesMap = 0;
float factor = 1;

float Rules::getRule(QString ruleName)
{
    return rulesMap->value(ruleName);
}

void Rules::openRulesFile(QString fileName, QPlainTextEdit *debug)
{
    if(rulesMap != 0)
        rulesMap->clear();

    rulesMap = new QMap<QString, float>();

    QFile file(fileName);

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        debug->appendHtml("<font color='red'>Failed openning: " + fileName + "</font>");
        return;
    }

    QTextStream in(&file);
    while (!in.atEnd())
    {
        QString ruleName, ruleValue, eqSignal;
        in >> ruleName;
        in >> eqSignal;
        in >> ruleValue;

        debug->appendPlainText("Rule: " + ruleName + " " + eqSignal + " " + ruleValue);
        rulesMap->insert(ruleName,ruleValue.toFloat());

    }
    debug->appendPlainText("Finished!");
    file.close();

    if(getRule("PO.W.1a") == 0.35f)
        factor = 10;
    else if(getRule("PO.W.1a") == 0.06f)
        factor = 40;

    foreach(QString key , rulesMap->keys())
        rulesMap->insert(key, rulesMap->value(key)*factor);
}


